import request from '@/utils/request'

/**
 * 上传攻略
 * @param {number} id - 商品ID
 * @param {FormData} data - 攻略数据（FormData格式）
 * @returns {Promise}
 */
export function uploadStrategy(id, data) {
  return request({
    url: `/api/v1/introduction/upload/${id}`,
    method: 'post',
    data: data,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

/**
 * 编辑攻略
 * @param {object} data - 攻略数据
 * @returns {Promise}
 */
export function updateStrategy(data) {
  return request({
    url: '/api/v1/introduction',
    method: 'post',
    data: data
  })
}

/**
 * 获取攻略列表
 * @param {object} query - 查询参数
 * @returns {Promise}
 */
export function listStrategies(query) {
  return request({
    url: '/api/v1/introduction/list',
    method: 'get',
    params: query
  })
}

/**
 * 获取攻略详情
 * @param {number} id - 攻略ID
 * @returns {Promise}
 */
export function getStrategy(id) {
  return request({
    url: `/api/v1/introduction/${id}`,
    method: 'get'
  })
}

/**
 * 删除攻略
 * @param {number|Array|string} ids - 攻略ID、ID数组或逗号分隔的ID字符串
 * @returns {Promise}
 */
export function delStrategy(ids) {
  // 如果是数组，转换为逗号分隔的字符串
  const idsParam = Array.isArray(ids) ? ids.join(',') : String(ids)
  
  return request({
    url: `/api/v1/introduction/${idsParam}`,
    method: 'delete',
    data: { ids: idsParam }
  })
}

/**
 * 获取产品策略
 * @param {number} id - 策略ID
 * @param {object} params - 查询参数
 * @param {string[]} [params.ageTags] - 年龄标签 (age_0_12,age_12_18,age_18_35,age_35_45,age_45_60,age_60_plus)
 * @param {string[]} [params.genderTags] - 性别标签 (male,female)
 * @param {string[]} [params.occasionTags] - 场合标签 (traditional_festival,birthday,anniversary,graduation,moving,everyday_gift,fm_festival,children_day,com_occasions,business)
 * @param {string[]} [params.relationshipTags] - 关系标签 (lover,elder,children,relative,friend,colleague,client)
 * @param {string[]} [params.typeTags] - 类型标签 (jewelry,life,skincare,food,health,art,fun,confession,daily_gift,digital,men_essentials,gift_box,ornament,tea_wine,custom_gift,couple,toy,book,science_puzzle,doll,dad_gift,mom_gift)
 * @returns {Promise}
 */
export function getProductStrategy(id, params) {
  // 确保ID是有效的
  if (!id) {
    return Promise.reject(new Error('策略ID不能为空'));
  }
  
  // 处理数组参数
  const processedParams = { ...params };
  
  // 将数组参数转换为字符串，如果它们不是已经是字符串的话
  ['ageTags', 'genderTags', 'occasionTags', 'relationshipTags', 'typeTags'].forEach(key => {
    if (processedParams[key] && Array.isArray(processedParams[key])) {
      processedParams[key] = JSON.stringify(processedParams[key]);
    }
  });
  
  return request({
    url: `/api/v1/products/strategy/${id}`,
    method: 'get',
    params: processedParams
  })
}
